package cn.b.sky.admin.service.impl;

import cn.b.sky.admin.dao.InitializationScriptDao;
import cn.b.sky.admin.service.InitializationScriptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("initializationScriptService")
public class InitializationScriptServiceImpl implements InitializationScriptService {

  private static final String SITECODE = "1310";

  private static final String ADMINUSER = "ADMIN";

  private static final String SA = "SA";

  @Resource
  InitializationScriptDao initializationScriptDao;

  @Override
  public String getDataScript(String[] tableNames, String functionGroup) {
    String sql = "";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String dateTime = sdf.format(new Date());
    String date = df.format(new Date());
    for (String tableName : tableNames) {
      switch (tableName) {
      case "SA_USER":
        sql = "insert into SA_USER (USERCODE, USERTEXT, PASSWORD, CREATEUSER, CREATEDATETIME, MODIFYUSER, MODIFYDATETIME)values ('SA', '系统管理员', '', '"
            + SA + "', to_date('" + dateTime + "', 'yyyy-MM-dd HH24:mm:ss'), '" + SA + "', to_date('" + dateTime
            + "', 'yyyy-MM-dd HH24:mm:ss'));";
        break;
      case "SY_USER":
        sql = "insert into SY_USER (SITECODE, USERCODE, USERTEXT, PASSWORD, FROMDATE, TODATE, PASSWORDCHANGEDATE, PASSWORDERRORTIMES, LANGUAGECODE, TIMEZONECODE, USERTYPEECODE, LOCKFLAG, CREATEUSER, CREATEDATETIME, MODIFYUSER, MODIFYDATETIME, SYSTEMFLAG, CONTACTNAME, BIRTHDATE, TELEPHONE, MOBILE, FAX, EMAIL, POSTCODE, USERNAME, ADDRESS)values ('"
            + SITECODE + "', '" + ADMINUSER + "', '管理员', '5690dddfa28ae085d23518a035707282', to_date('" + date
            + "', 'yyyy-MM-dd'), to_date('31-12-9999', 'dd-mm-yyyy'), null, null, 'zh', 'Asia/Shanghai', 'USER', 0, '"
            + SA + "', to_date('" + dateTime + "', 'yyyy-MM-dd HH24:mm:ss'), '" + SA + "', to_date('" + dateTime
            + "', 'yyyy-MM-dd HH24:mm:ss'), 1, null, null, null, null, null, null, null, '" + ADMINUSER + "@" + SITECODE
            + "', null);";
        break;
      case "SY_ROLE":
        sql = "insert into SY_ROLE (SITECODE, ROLECODE, ROLETEXT, MEMO, LOCKFLAG, CREATEUSER, CREATEDATETIME, MODIFYUSER, MODIFYDATETIME, SYSTEMFLAG)values ('"
            + SITECODE + "', '" + ADMINUSER + "', '管理员', '', 0, '" + SA + "', to_date('" + dateTime
            + "', 'yyyy-MM-dd HH24:mm:ss'), '" + SA + "',to_date('" + dateTime + "', 'yyyy-MM-dd HH24:mm:ss'), 1);";
        break;
      case "SY_USERROLE":
        sql = "insert into SY_USERROLE (SITECODE, USERCODE, ROLECODE, CREATEUSER, CREATEDATETIME)values ('" + SITECODE
            + "', '" + ADMINUSER + "', '" + ADMINUSER + "', '" + SA + "', to_date('" + dateTime
            + "', 'yyyy-MM-dd HH24:mm:ss'));";
        break;
      default:
        List<Map<String, Object>> datas = initializationScriptDao.getAllDataForCommonTable(SITECODE, tableName,
            functionGroup, ADMINUSER);
        for (Map<String, Object> data : datas) {
          String columns = "";
          String values = "";
          for (Map.Entry<String, Object> entry : data.entrySet()) {
            columns += entry.getKey() + ",";
            if ("java.sql.Timestamp".equals(entry.getValue().getClass().getName())) {
              values += "to_date('" + sdf.format(entry.getValue()) + "', 'yyyy-MM-dd HH24:mm:ss')" + ",";
            } else if ("java.lang.String".equals(entry.getValue().getClass().getName())) {
              values += "'" + entry.getValue() + "'" + ",";
            } else {
              values += entry.getValue() + ",";
            }
          }
          columns = columns.substring(0, columns.length() - 1);
          values = values.substring(0, values.length() - 1);
          sql += "insert into " + tableName + "(" + columns + ")" + " values (" + values + ");" + "\r\n";
        }
        break;
      }
    }
    return sql;
  }

}
